package com.kehutong.wxapi.service.impl;

import com.kehutong.wxapi.request.*;
import com.kehutong.wxapi.response.*;
import com.kehutong.wxapi.service.UserService;
import com.kehutong.wxapi.util.KhtClient;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;
import org.coraframework.util.Objects;

/**
 * 成员管理服务实现类
 * @author: liukunlong
 * @date: 2021-03-08 15:50
 */
public class UserServiceImpl implements UserService {
    @Inject
    private KhtClient khtClient;

    /**
     * 创建成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN
     * @param userCreateRequest 创建成员请求对象
     * @return 创建成员响应对象
     * @throws Exception 异常
     */
    public UserCreateResponse userCreate(UserCreateRequest userCreateRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/create", http -> {
            http.addParam("access_token", userCreateRequest.getAccessToken());
            http.setBody(JSON.toJSONString(userCreateRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserCreateResponse.class);
    }

    /**
     * 读取成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID
     * @param userGetRequest 读取成员请求对象
     * @return 读取成员响应对象
     * @throws Exception 异常
     */
    public UserGetResponse userGet(UserGetRequest userGetRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/get", http -> {
            http.addParam("access_token", userGetRequest.getAccessToken());
            http.addParam("userid", userGetRequest.getUserid());
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserGetResponse.class);
    }

    /**
     * 更新成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=ACCESS_TOKEN
     * @param userUpdateRequest 更新成员请求对象
     * @return 更新成员响应对象
     * @throws Exception 异常
     */
    public UserUpdateResponse userUpdate(UserUpdateRequest userUpdateRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/update", http -> {
            http.addParam("access_token", userUpdateRequest.getAccessToken());
            http.setBody(JSON.toJSONString(userUpdateRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserUpdateResponse.class);
    }

    /**
     * 删除成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=USERID
     * @param userDeleteRequest 删除成员请求对象
     * @return 删除成员响应对象
     * @throws Exception 异常
     */
    public UserDeleteResponse UserDelete(UserUpdateRequest userDeleteRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/delete", http -> {
            http.addParam("access_token", userDeleteRequest.getAccessToken());
            http.addParam("userid", userDeleteRequest.getUserid());
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserDeleteResponse.class);
    }


    /**
     * 批量删除成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token=ACCESS_TOKEN
     * @param userBatchDeleteRequest 批量删除成员请求对象
     * @return 批量删除成员响应对象
     * @throws Exception 异常
     */
    public UserBatchDeleteResponse userBatchDelete(UserBatchDeleteRequest userBatchDeleteRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/batchdelete", http -> {
            http.addParam("access_token", userBatchDeleteRequest.getAccessToken());
            http.setBody(JSON.toJSONString(userBatchDeleteRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserBatchDeleteResponse.class);
    }

    /**
     * 获取部门成员
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD
     * @param userSimpleListRequest 获取部门成员请求对象
     * @return 获取部门成员响应对象
     * @throws Exception 异常
     */
    public UserSimpleListResponse userSimpleList(UserSimpleListRequest userSimpleListRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/simplelist", http -> {
            http.addParam("access_token", userSimpleListRequest.getAccessToken());
            http.addParam("department_id", String.valueOf(userSimpleListRequest.getDepartmentId()));
            if(Objects.nonEmpty(userSimpleListRequest.getFetchChild())){
                http.addParam("fetch_child", userSimpleListRequest.getFetchChild());
            }
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserSimpleListResponse.class);
    }

    /**
     * 获取部门成员详情
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD
     * @param userListRequest 获取部门成员详情请求对象
     * @return 获取部门成员详情响应对象
     * @throws Exception 异常
     */
    public UserListResponse userList(UserListRequest userListRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/list", http -> {
            http.addParam("access_token", userListRequest.getAccessToken());
            http.addParam("department_id", String.valueOf(userListRequest.getDepartmentId()));
            if(Objects.nonEmpty(userListRequest.getFetchChild())){
                http.addParam("fetch_child", userListRequest.getFetchChild());
            }
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserListResponse.class);
    }

    /**
     * userid与openid互换
     * 请求地址： https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=ACCESS_TOKEN
     * @param userConvertToOpenidRequest userid与openid互换请求对象
     * @return userid与openid互换响应对象
     * @throws Exception 异常
     */
    public UserConvertToOpenidResponse userConvertToOpenid(UserConvertToOpenidRequest userConvertToOpenidRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/user/convert_to_openid", http -> {
            http.addParam("access_token", userConvertToOpenidRequest.getAccessToken());
            userConvertToOpenidRequest.setAccessToken(null);
            http.setBody(JSON.toJSONString(userConvertToOpenidRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), UserConvertToOpenidResponse.class);
    }
}
